package com.service;

import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.E_XPSWHPTH_NOT_FOUND_RECORD;
import static com.def.GenericDef.E_XPSWHPTH_OUT_PATH_NOT_EXIST;
import static com.def.GenericDef.E_XPSWHPTH_RECORD_ALREADY_EXIST;
import static com.def.GenericDef.E_XPSWHPTH_SWH_PATH_NOT_EXIST;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.T_XPSWITCH;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisPathDao;
import com.dao.IBisPathSwhDao;
import com.dao.IBisPathiDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_path_swh;
import com.model.Bis_path_swhId;
import com.tx.Xpswhpth.XpswhpthI;
import com.tx.Xpswhpth.XpswhpthIA;
import com.tx.Xpswhpth.XpswhpthO;
import com.tx.Xpswhpth.XpswhpthOA;
import com.util.MqOpe;

@Scope("prototype")
@Service("XpswtpthService")
public class XpswtpthService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpswtpthService.class);
	private static String TX_NAME = T_XPSWITCH;

	@Autowired
	private MqOpe mqOpe;

	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private String gEvtSeqID;

	
//	@Autowired
	private XpswhpthI xpswhpthI = new XpswhpthI();
	
//	@Autowired
	private XpswhpthO xpswhpthO = new XpswhpthO(); 
	
	@Autowired
	private IBisPathDao bisPathDao;
	@Autowired
	private IBisPathiDao bisPathiDao;
	@Autowired
	private IBisPathSwhDao bisPathSwhDao;

	public String subMainProc(String strInMsg) {
		long rtnCode = _NORMAL;
		logger.info("InTrx is "+ strInMsg );
		try{
//			AppContext.clear();
//			AppContext.setCurrServiceName(this.getClass().getSimpleName());
			xpswhpthI = (XpswhpthI) mqOpe.inStr2Obj(strInMsg,xpswhpthI);
			xpswhpthO.setTrx_id("XPSWHPTH");
			xpswhpthO.setType_id("O");
			char action_flg = xpswhpthI.getAction_flg().charAt(0);
			switch(action_flg){
				case ACTION_FLG_QUERY   :  queryByCondition()  ; break;   
				case ACTION_FLG_ADD     :  addFnc()            ; break;
				case ACTION_FLG_UPDATE  :  updateFnc()         ; break;
				case ACTION_FLG_DELETE  :  deleteFnc()         ; break;
				default                 :  errActionFnc();   
			}
		    if( RETURN_CODE_OK.equals(xpswhpthO.getRtn_code()) ){
		    	bisPathSwhDao.flushCurSession();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}		
		}catch(Exception ex){
			xpswhpthO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpswhpthO.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}finally{
			logger.info(mqOpe.obj2Str(xpswhpthO));
		}
		return mqOpe.obj2Str(xpswhpthO);
	}
	public void queryByCondition(){
		XpswhpthIA iary = xpswhpthI.getIary().get(0);
		String hql ="FROM Bis_path_swh where 1=1";
		List<XpswhpthOA> oaryList = new ArrayList<XpswhpthOA>();
		if(iary.getFr_path_id_fk()!=null){
			hql = hql + " and fr_path_id_fk ='" + iary.getFr_path_id_fk() +"'";
		}
		if(iary.getFr_path_ver_fk()!=null){
			hql = hql + " and fr_path_ver_fk='" + iary.getFr_path_ver_fk()+"'";
		}
		if(iary.getFr_ope_no_fk()!=null){
			hql = hql + " and fr_ope_no_fk='" + iary.getFr_ope_no_fk()+"'";
		}
		if(iary.getSwh_id()!=null){
			hql = hql + " and swh_id='" + iary.getSwh_id()+"'";
		}
		if(iary.getSwh_typ()!=null){
			hql = hql +" and swh_typ='" + iary.getSwh_typ()+"'";
		}
		List<Bis_path_swh> bisPathSwhList = bisPathSwhDao.find(hql);
		for(Bis_path_swh bis_path_swh:bisPathSwhList){
			XpswhpthOA oary = new XpswhpthOA();
			oary.setFr_ope_no_fk( bis_path_swh.getId().getFr_ope_no_fk());
			oary.setFr_path_id_fk( bis_path_swh.getId().getFr_path_id_fk());
			oary.setFr_path_ver_fk( bis_path_swh.getId().getFr_path_ver_fk());
			oary.setSwh_id( bis_path_swh.getId().getSwh_id());
			oary.setSwh_typ( bis_path_swh.getId().getSwh_typ());
			oary.setSwh_dsc( bis_path_swh.getSwh_dsc());
			oary.setSwh_path_id_fk( bis_path_swh.getSwh_path_id_fk());
			oary.setSwh_path_ver_fk( bis_path_swh.getSwh_path_ver_fk());
			oary.setOut_ope_no_fk( bis_path_swh.getOut_ope_no_fk());
			oary.setOut_path_id_fk( bis_path_swh.getOut_path_id_fk());
			oary.setOut_path_ver_fk( bis_path_swh.getOut_path_ver_fk());
			oaryList.add(oary);
		}
		xpswhpthO.setOary(oaryList);
		xpswhpthO.setTbl_cnt(oaryList.size());
		xpswhpthO.setRtn_code(RETURN_CODE_OK);
	}
	public void addFnc(){
		List<XpswhpthIA> iaryList = xpswhpthI.getIary();
		for(XpswhpthIA iary : iaryList){
			Bis_path_swhId pathSwhId = new Bis_path_swhId();
			
			//检查swh_id长度，只能是0~~9之间的整数
			String swh_id = iary.getSwh_id();
			if (swh_id.length() != 1) {
				xpswhpthO.setRtn_code(E_XPSWHPTH_RECORD_ALREADY_EXIST+"");
				xpswhpthO.setRtn_mesg("分支代码只能为0~~9之间的整数");
				return ;
			}
			
			int swh_id_int; 
			try {
				swh_id_int = Integer.valueOf(swh_id);
			} catch (NumberFormatException e) {
				xpswhpthO.setRtn_code(E_XPSWHPTH_RECORD_ALREADY_EXIST+"");
				xpswhpthO.setRtn_mesg("分支代码只能为0~~9之间的整数");
				return ;
			}
			
			if (swh_id_int > 9 || swh_id_int < 0) {
				xpswhpthO.setRtn_code(E_XPSWHPTH_RECORD_ALREADY_EXIST+"");
				xpswhpthO.setRtn_mesg("分支代码只能为0~~9之间的整数");
				return ;
			}
			
			iary.setSwh_id(String.valueOf(swh_id_int));
			
			pathSwhId.setFr_ope_no_fk(iary.getFr_ope_no_fk());
			pathSwhId.setFr_path_id_fk(iary.getFr_path_id_fk());
			pathSwhId.setFr_path_ver_fk(iary.getFr_path_ver_fk());
			pathSwhId.setSwh_id(iary.getSwh_id());
			pathSwhId.setSwh_typ(iary.getSwh_typ());
			Bis_path_swh bis_path_swh = bisPathSwhDao.get(pathSwhId);
			if(bis_path_swh!=null){
				xpswhpthO.setRtn_code(E_XPSWHPTH_RECORD_ALREADY_EXIST+"");
				xpswhpthO.setRtn_mesg("该记录已经存在");
				return ;
			}
			//Check swh pathi exist or not
			if( bisPathiDao.getPathInfo(iary.getSwh_path_id_fk(),iary.getSwh_path_ver_fk())==null){
				xpswhpthO.setRtn_code(E_XPSWHPTH_SWH_PATH_NOT_EXIST+"");
				xpswhpthO.setRtn_mesg("该分支线路["+iary.getSwh_path_id_fk()+"],["+iary.getSwh_path_ver_fk()+
						"]"+"不存在，请检查");
				return ;
			}
			
			//fr_path_id = out_path_id
			iary.setOut_path_id_fk(iary.getFr_path_id_fk());
			iary.setOut_path_ver_fk(iary.getFr_path_ver_fk());
			
			//check out path
			if(bisPathDao.getPathOpeInfo(iary.getOut_path_id_fk(), iary.getOut_path_ver_fk(), 
			   iary.getOut_ope_no_fk()) == null ){
				xpswhpthO.setRtn_code(E_XPSWHPTH_OUT_PATH_NOT_EXIST+"");
				xpswhpthO.setRtn_mesg("out path["+iary.getOut_path_id_fk()+
						"],path_ver["+iary.getOut_path_ver_fk()+
						"],out_ope_no["+iary.getOut_path_ver_fk()+
						"] not exist,please check");
				return ;
				
			};
			bis_path_swh = new Bis_path_swh();
			bis_path_swh.setId(pathSwhId);
			bis_path_swh.setSwh_path_id_fk(iary.getSwh_path_id_fk());
			bis_path_swh.setSwh_path_ver_fk(iary.getSwh_path_ver_fk());
			bis_path_swh.setSwh_dsc(iary.getSwh_dsc());
			bis_path_swh.setOut_ope_no_fk(iary.getOut_ope_no_fk());
			bis_path_swh.setOut_path_id_fk(iary.getOut_path_id_fk());
			bis_path_swh.setOut_path_ver_fk(iary.getOut_path_ver_fk());
			bisPathSwhDao.save(bis_path_swh);
		}
		xpswhpthO.setRtn_code(RETURN_CODE_OK);
	}
	public void updateFnc(){
		
		xpswhpthO.setRtn_code(RETURN_CODE_OK);
	}
	public void deleteFnc(){
		List<XpswhpthIA> iaryList = xpswhpthI.getIary();
		for(XpswhpthIA iary : iaryList){
			Bis_path_swhId pathSwhId = new Bis_path_swhId();
			pathSwhId.setFr_ope_no_fk(iary.getFr_ope_no_fk());
			pathSwhId.setFr_path_id_fk(iary.getFr_path_id_fk());
			pathSwhId.setFr_path_ver_fk(iary.getFr_path_ver_fk());
			
			int swh_id = 0;
			
			try {
				swh_id = Integer.valueOf(iary.getSwh_id());
			} catch (NumberFormatException e) {
				xpswhpthO.setRtn_code(E_XPSWHPTH_NOT_FOUND_RECORD+"");
				xpswhpthO.setRtn_mesg("分支号不是整数!");
				return ;
			}
			
			pathSwhId.setSwh_id(String.valueOf(swh_id));
			pathSwhId.setSwh_typ(iary.getSwh_typ());
			Bis_path_swh bis_path_swh = bisPathSwhDao.get(pathSwhId);
			if(bis_path_swh==null){
				xpswhpthO.setRtn_code(E_XPSWHPTH_NOT_FOUND_RECORD+"");
				xpswhpthO.setRtn_mesg("该分支信息没找到");
				return ;
			}
			//Check swh pathi exist or not
			bisPathSwhDao.delete(bis_path_swh);
		}
		xpswhpthO.setRtn_code(RETURN_CODE_OK);
	}
	public void errActionFnc(){
		xpswhpthO.setRtn_code("0000009");
		xpswhpthO.setRtn_mesg("Invalid Action_flg");
	}

}
